package io.zpy.quartz;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.zpy.service.TrackTopicsService;
import lombok.Data;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.io.Serializable;

@Data
@Component
public class ScheduleJob implements Serializable, Job {

    @Autowired
    private TrackTopicsService trackTopicsService;

    private static ScheduleJob scheduleJob;

    @PostConstruct
    private void init(){
        scheduleJob = this;
        scheduleJob.trackTopicsService = this.trackTopicsService;
    }

    private static final long serialVersionUID = 1L;
    private static final String JOB_MAP_KEY = "self";
    public static final String STATUS_RUNNING = "1";
    public static final String STATUS_NOT_RUNNING = "0";
    public static final String CONCURRENT_IS = "1";
    public static final String CONCURRENT_NOT = "0";

    /**
     * 任务名称
     */
    private String jobName;
    /**
     * 任务分组
     */
    private String jobGroup;
    /**
     * 任务状态 是否启动任务
     */
    private String jobStatus;
    /**
     * cron表达式
     */
    private String cronExpression;
    /**
     * 描述
     */
    private String description;
    /**
     * 任务执行时调用哪个类的方法 包名+类名
     */
    private String beanClass;
    /**
     * 任务是否有状态
     */
    private String isConcurrent;

    /**
     * Spring bean
     */
    private String springBean;

    /**
     * 任务调用的方法名
     */
    private String methodName;
    /**
     * 订单id
     */
    private Integer pid;
    /**
     * 用户id
     */
    private String uid;
    /**
     * 订单关键词
     */
    private String keywords;
    /**
     * 收件人
     */
    private String toAddress;
    /**
     * 追踪频率
     */
    private String frequency;
    /**
     * 追踪数量
     */
    private String trackAmount;
    /**
     * 创建时间
     */
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private String createDate;
    /**
     * 更新时间
     */
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private String updateDate;
    /**
     * 标志位（用来判断是新增还是更新）
     */
    private String flag;


    @JsonIgnore
    private JobDataMap dataMap = new JobDataMap();


    public JobDataMap getDataMap() {
        if (dataMap.size() == 0) {
            dataMap.put(JOB_MAP_KEY, this);
        }
        return dataMap;
    }


    public JobKey getJobKey() {
        return JobKey.jobKey(jobName, jobGroup);// 任务名称和组构成任务key
    }

    public static ScheduleJob getJobFromMap(JobDataMap dataMap) {
        return (ScheduleJob) dataMap.get(JOB_MAP_KEY);
    }

    @Override
    @Transactional(rollbackFor = {Throwable.class})
    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("开始>>>>>>>>>>>>>>>>>>>>>>>>>");

        JobDataMap dataMap = context.getJobDetail().getJobDataMap();
        //System.out.println("当前执行JOB:" + dataMap.get(JOB_MAP_KEY));
        ScheduleJob sj = (ScheduleJob) dataMap.get(JOB_MAP_KEY);
        scheduleJob.trackTopicsService.quartzJobTest(sj);

        System.out.println("结束>>>>>>>>>>>>>>>>>>>>>>>>>>");
    }

    @Override
    public String toString() {
        return "ScheduleJob{" +
                "jobName='" + jobName + '\'' +
                ", jobGroup='" + jobGroup + '\'' +
                ", jobStatus='" + jobStatus + '\'' +
                ", cronExpression='" + cronExpression + '\'' +
                ", description='" + description + '\'' +
                ", beanClass='" + beanClass + '\'' +
                ", isConcurrent='" + isConcurrent + '\'' +
                ", springBean='" + springBean + '\'' +
                ", methodName='" + methodName + '\'' +
                ", pid='" + pid + '\'' +
                ", keywords='" + keywords + '\'' +
                ", toAddress='" + toAddress + '\'' +
                ", frequency='" + frequency + '\'' +
                ", trackAmount='" + trackAmount + '\'' +
                ", createDate='" + createDate + '\'' +
                ", updateDate='" + updateDate + '\'' +
                ", flag='" + flag + '\'' +
                '}';
    }


}